<?php
defined('IN_XLP') or exit('Access Denied');

class articleAction extends commonAction {

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
        $article_type = array( 1 => '关于我们', 2 => '联系我们', 3 => '网站公告', 4 => '广告服务', 5 => '手续费一览', 6 => '最新活动' );
        $this->assign('article_type' , $article_type);
    }


    /*
     * 文章列表
     */
    public function index() {
        $title = $this->_get('q');
        $p = $this->_getid('p', 1);
        $cid = $this->_getid('cid' , 0);
        $where = array();

        if ($title) {
            $where['LIKE'] = array('title' => parent::safeSearch($title));
        }

        if($cid) {
            $where['cid'] = $cid;
        }
        $rs = M('article')->where($where)->page($p)->order('px DESC')->findAll();
        $this->assign(array('rs' => $rs, 'p' => $p, 'total' => M('article')->getTotal(),'cid' => $cid));
        $this->display();
    }


    /*
     * 添加问题
     */
    public function add() {
        if(isHave($_POST)) {
            $data['title'] = $this->_post('title' , '');
            if(!isHave($data['title'])) {
                $this->JsonReturn('请填写文章标题');
            }
            $data['cid'] = $this->_postid('cid');
            $data['content'] = $this->_postContent('content');
            $data['status'] = $this->_postid('status');
            $data['infotime'] = TIME;
            $result = M('article')->insert($data);
            if($result) {
                $this->JsonReturn('添加成功', null, 1);
            } else {
                $this->JsonReturn('添加失败', null, 0);
            }
        }else{
            $this->display();
        }
    }

    /*
     * 编辑商户
     */
    public function edit() {
        if(isHave($_POST)){
            $id = $this->_postid('id');
            if(!isHave($id)){
                $this->JsonReturn('参数丢失');
            }
            $data['title'] = $this->_post('title' , '');
            if(!isHave($data['title'])) {
                $this->JsonReturn('文章名称不能为空');
            }
            $data['cid'] = $this->_post('cid');
            $data['content'] = $this->_postContent('content');
            $data['status'] = $this->_postid('status');
            $result = M('article')->update($data , array('id' => $id));
            if($result) {
                $this->JsonReturn('编辑成功', null, 1);
            } else {
                $this->JsonReturn('编辑失败', null, 0);
            }
        }else{
            $id = $this->_getid('id' , 0);
            if(!isHave($id)) {
                showError('参数丢失');
            }
            $map['id'] = $id;
            $info = M('article')->where($map)->find();
            if(!$info) {
                showError('内容不存在');
            }
            $this->assign('info', $info);
            $this->display();
        }
    }

    function delete() {
        $id = $this->_postid('id' , 0);
        if(!isHave($id)) {
            $this->JsonReturn('参数丢失');
        }
        $map[ 'id' ] = $id;
        $result = M('article')->delete($map);
        if($result) {
            $this->JsonReturn('ok',null ,1);
        }else{
            $this->JsonReturn("操作失败");
        }
    }
}